package com.binc.testspring.util;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import static java.util.Calendar.HOUR_OF_DAY;

public class DateUtil {

    public static String[] parsePatterns = new String[]{"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", "yyyy/MM/dd",
            "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss",
            "yyyy.MM.dd HH:mm", "yyyy.MM", "MMdd", "yyyyMMdd"};

    /**
     * 获取当天0点时间戳
     * author: Feng
     */
    public static Long getZeroTime() {
        long zero = LocalDate.now().atStartOfDay().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        return zero;
    }

    public static Long getZeroTime(Long current) {
        long zero = current / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();
        return zero;
    }

    /**
     * 获取当天23.59.59时间戳
     * author: Feng
     */
    public static Long getLastTime() {
        long lastTime = LocalDate.now().atTime(23, 59, 59, 0).toInstant(ZoneOffset.of("+8")).toEpochMilli();
        return lastTime;
    }


    /**
     * 获取当月天数
     *
     * @param monthStartTime
     * @return
     */
    public static Integer getDayNumFormCurrentMonth(Long monthStartTime) {
        Calendar calendar = getCurrentMonthCalendar(monthStartTime);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取{@link Calendar}
     *
     * @param time
     * @return
     */
    private static Calendar getCurrentMonthCalendar(Long time) {
        Calendar calendar = getBaseCalendar(new Date(time));
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return calendar;
    }


    /**
     * 获取一个基础的 Calendar
     *
     * @param time
     * @return
     */
    public static Calendar getBaseCalendar(Date time) {
        return getBaseCalendar(time.getTime());
    }


    public static Calendar getBaseCalendar(Long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        calendar.set(HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar;
    }


    /**
     * 获取当前最大天数
     *
     * @param year
     * @param month
     * @return
     */
    public static int getDayNumFormCurrentMonth(int year, int month) {
        Calendar baseCalendar = getBaseCalendar(System.currentTimeMillis());
        baseCalendar.set(Calendar.YEAR, year);
        baseCalendar.set(Calendar.MONTH, month);
        return baseCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * @Description: 时间戳转时间
     * @Param:
     * @return:
     * @Author: Feng
     * @Date:
     */
    public static String TsToDate(Long ts) {
        if (ts == null || ts.compareTo(1000L) < 0) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sd = sdf.format(new Date(Long.parseLong(String.valueOf(ts))));
        return sd;
    }

    public static Date longToDate(long currentTime) {
        Date date = new Date(currentTime); // 根据long类型的毫秒数生命一个date类型的时间
        return date;
    }

    public static String longToStr(long time, String pattern) {
        return DateFormatUtils.format(longToDate(time), pattern);
    }

    /**
     * 获取现在时间
     *
     * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
     */
    public static String getStringDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }
}
